﻿namespace Yom.Data
{
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    /// <summary>
    /// 管理员数据操作
    /// 
    /// 创建: 邱柯皓 2009/01/19 10:00
    /// </summary>
    public class Admin
    {
        #region 私有成员定义
        private readonly DBHelper db = new DBHelper();
        #endregion

        #region 构造函数
        public Admin() { }
        #endregion

        #region 私有方法
        /// <summary>
        /// 通过DataReader获取管理员实体对象
        /// 
        /// 创建: 邱柯皓 2009/01/19 10:03
        /// </summary>
        /// <param name="dr">DataReader对象</param>
        /// <returns>返回管理员实体对象</returns>
        private Yom.Model.Admin GetModel(SqlDataReader dr)
        {
            Yom.Model.Admin model = new Yom.Model.Admin();
            model.AdminID = db.GetInt(dr["AdminID"]);
            model.Email = db.GetString(dr["Email"]);
            model.LoginName = db.GetString(dr["LoginName"]);
            model.LoginPass = db.GetString(dr["LoginPass"]);
            model.UserName = db.GetString(dr["UserName"]);

            return model;
        }

        /// <summary>
        /// 通过DataReader获取管理员列表
        /// 
        /// 创建: 邱柯皓 2009/01/19 13:21
        /// </summary>
        /// <param name="dr">DataReader</param>
        /// <returns>返回管理员列表</returns>
        private List<Yom.Model.Admin> GetList(SqlDataReader dr)
        {
            List<Yom.Model.Admin> lsit = new List<Yom.Model.Admin>();

            while (dr.Read())
                lsit.Add(GetModel(dr));

            return lsit;
        }
        #endregion

        #region 公共方法
        /// <summary>
        /// 添加管理员
        /// 
        /// 创建: 邱柯皓 2009/01/19 10:12
        /// </summary>
        /// <param name="model">管理员实体对象</param>
        /// <returns>返回成功标识</returns>
        public bool Add(Yom.Model.Admin model)
        {
            StringBuilder strSQL = new StringBuilder();
            strSQL.Append("INSERT INTO [Yom_Admin]([AdminID], [LoginName], [LoginPass], [UserName], [Email])");
            strSQL.Append(" VALUES (@AdminID, @LoginName, @LoginPass, @UserName, @Email)");
            SqlParameter[] cmdParms = {
                db.CreateInSqlParameter("@AdminID", DbType.Int32, model.AdminID),
                db.CreateInSqlParameter("@LoginName", DbType.String, model.LoginName),
                db.CreateInSqlParameter("@LoginPass", DbType.String, model.LoginPass),
                db.CreateInSqlParameter("@UserName", DbType.String, model.UserName),
                db.CreateInSqlParameter("@Email", DbType.String, model.Email)};

            return db.ExecuteNonQuery(CommandType.Text, strSQL.ToString(), cmdParms) > 0;
        }

        /// <summary>
        /// 修改管理员
        /// 
        /// 创建: 邱柯皓 2009/01/19 10:34
        /// </summary>
        /// <param name="model">管理员实体对象</param>
        /// <returns>返回成功标识</returns>
        public bool Update(Yom.Model.Admin model)
        {
            StringBuilder strSQL = new StringBuilder();
            strSQL.Append("UPDATE [Yom_Admin] SET [LoginName]=@LoginName, [LoginPass]=@LoginPass, ");
            strSQL.Append("[UserName]=@UserName, [Email]=@Email WHERE [AdminID]=@AdminID");
            SqlParameter[] cmdParms = {
                db.CreateInSqlParameter("@LoginName", DbType.String, model.LoginName),
                db.CreateInSqlParameter("@LoginPass", DbType.String, model.LoginPass),
                db.CreateInSqlParameter("@UserName", DbType.String, model.UserName),
                db.CreateInSqlParameter("@Email", DbType.String, model.Email),
                db.CreateInSqlParameter("@AdminID", DbType.Int32, model.AdminID)};

            return db.ExecuteNonQuery(CommandType.Text, strSQL.ToString(), cmdParms) > 0;
        }

        /// <summary>
        /// 删除管理员
        /// 
        /// 创建: 邱柯皓 2009/01/19 10:39
        /// </summary>
        /// <param name="AdminID">管理员编号</param>
        /// <returns>返回成功标识</returns>
        public bool Delete(int AdminID)
        {
            StringBuilder strSQL = new StringBuilder();
            strSQL.Append("DELETE FROM [Yom_Admin] WHERE [AdminID]=@AdminID");
            SqlParameter[] cmdParms = { db.CreateInSqlParameter("@AdminID", DbType.Int32, AdminID) };

            return db.ExecuteNonQuery(CommandType.Text, strSQL.ToString(), cmdParms) > 0;
        }

        /// <summary>
        /// 获取管理员信息
        /// 
        /// 创建: 邱柯皓 2009/01/19 10:42
        /// </summary>
        /// <param name="AdminID">管理员编号</param>
        /// <returns>返回管理员实体对象</returns>
        public Yom.Model.Admin GetModel(int AdminID)
        {
            StringBuilder strSQL = new StringBuilder();
            Yom.Model.Admin model = new Yom.Model.Admin();
            strSQL.Append("SELECT * FROM [Yom_Admin] WHERE [AdminID]=@AdminID");
            SqlParameter[] cmdParms = { db.CreateInSqlParameter("@AdminID", DbType.Int32, AdminID) };

            using (SqlDataReader dr = db.ExecuteReader(CommandType.Text, strSQL.ToString(), cmdParms))
            {
                while (dr.Read())
                    model = GetModel(dr);

                return model;
            }
        }

        /// <summary>
        /// 获取管理员信息
        /// 
        /// 创建: 邱柯皓 2009/02/19 19:21
        /// </summary>
        /// <param name="LoginName">管理员登录名</param>
        /// <param name="LoginPass">管理员登录密码</param>
        /// <returns>返回管理员实体对象</returns>
        public Yom.Model.Admin GetModel(string LoginName, string LoginPass)
        {
            StringBuilder strSQL = new StringBuilder();
            Yom.Model.Admin model = new Yom.Model.Admin();
            strSQL.Append("SELECT * FROM [Yom_Admin] WHERE [LoginName]=@LoginName AND [LoginPass]=@LoginPass");
            SqlParameter[] cmdParms = {
                db.CreateInSqlParameter("@LoginName", DbType.String, LoginName),
                db.CreateInSqlParameter("@LoginPass", DbType.String, LoginPass)};

            using (SqlDataReader dr = db.ExecuteReader(CommandType.Text, strSQL.ToString(), cmdParms))
            {
                while (dr.Read())
                    model = GetModel(dr);

                return model;
            }
        }

        /// <summary>
        /// 获取管理员列表
        /// 
        /// 创建: 邱柯皓 2009/01/19 13:20
        /// </summary>
        /// <returns>返回管理员列表</returns>
        public List<Yom.Model.Admin> GetList()
        {
            StringBuilder strSQL = new StringBuilder("SELECT * FROM [Yom_Admin]");
            using (SqlDataReader dr = db.ExecuteReader(CommandType.Text, strSQL.ToString(), null))
            {
                List<Yom.Model.Admin> list = GetList(dr);

                return list;
            }
        }

        /// <summary>
        /// 分页获取管理员列表
        /// 
        /// 创建: 邱柯皓 2009/01/19 13:25
        /// </summary>
        /// <param name="pageSize">每页显示数量</param>
        /// <param name="pageIndex">当前页索引</param>
        /// <param name="fldSort">排序字段</param>
        /// <param name="condition">查询条件</param>
        /// <param name="sort">排序方式, true为正序, false为倒序</param>
        /// <returns>返回管理员列表</returns>
        public List<Yom.Model.Admin> GetList(int pageSize, int pageIndex, string fldSort, string condition, bool sort)
        {
            using (SqlDataReader dr = db.GetPager(pageSize, pageIndex, "[Yom_Admin]", fldSort, condition, sort))
            {
                List<Yom.Model.Admin> list = GetList(dr);

                return list;
            }
        }
        #endregion
    }
}